import time
import pymongo
import common.gyblog as log
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService

LOG = log.get_logger()
SERVICE = ChromeService(executable_path="../common/chromedriver.exe")
CONNECTION_STR = 'mongodb://192.168.0.196:27017/?readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false'
# CONNECTION_STR = 'mongodb://192.168.0.196:27017/'
DATA_BASE = 'javbook_new'  # 数据库名
DB_COL = 'javbook_new'  # 表名
SERIES = 'Caribbeancom'


def get_movies(url):
    # 创建 WebDriver 对象
    driver = webdriver.Chrome(service=SERVICE)
    # 导航到页面
    driver.get(url)
    # 使用 XPath 定位元素
    nums = driver.find_elements(By.XPATH, '//*[@id="PoShow_Box"]/div[position()]/div[4]/font')
    detail_urls = driver.find_elements(By.XPATH, '//*[@id="PoShow_Box"]/div[position()]/div[2]/a')
    len_num = len(nums)
    print('抓取nums完成:{}，数量为{}个'.format(url, str(len_num)))
    LOG.info('抓取nums完成:{}，数量为{}个'.format(url, str(len_num)))

    # 连接数据库
    db_client = pymongo.MongoClient(CONNECTION_STR)
    db_database = db_client[DATA_BASE]
    db_col = db_database[DB_COL]
    if len_num == len(detail_urls):
        for i in range(0, len(nums)):
            num = str(nums[i].text).split('/')[0].strip()
            year = str(nums[i].text).split('/')[1].strip().split('-')[0]
            detail_url = detail_urls[i].get_attribute('href')
            thz_tor = ''
            other_tor = ''
            sukebei_tor = ''
            my_dict = {'num': num, 'series': SERIES, 'year': year, 'detail_url': detail_url,
                       'thz_tor': thz_tor,
                       'sukebei_tor': sukebei_tor, 'other_tor': other_tor}
            db_col.insert_one(my_dict)
    time.sleep(1)
    db_client.close()
    driver.close()
    print('数据库已保存')
    LOG.info('数据库已保存')
    return len_num


if __name__ == '__main__':
    url = 'https://javbooks.com/serchinfo_uncensored/30/makersbt_{}.htm'

    for i in range(1, 93):
        while True:
            len_num = get_movies(url.format(str(i)))
            if len_num != 0:
                break
